﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MiniWeb.Core.Data;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Enums;
using MiniWeb.Task;
using MiniWeb.Web.Controllers.Admin.NewsTypeManager.ViewModels;
using WFramework.Extensions;
using WFramework.Mappers;
using WFramework.MvcPager;

namespace MiniWeb.Web.Controllers.Admin.NewsTypeManager
{

    public class NewsTypeManagerController : BaseController
    {
        private readonly NewsTypeTask _newstypeTask = new NewsTypeTask();

        public ActionResult List(int? rid,int pageIndex = 1, int pageSize = 20)
        {
            var pagedList = _newstypeTask.GetPagedList(rid, pageIndex, pageSize);


            ViewData["roottypelist"] = RootNewsTypeEnum.支付方式.ToSelectListForAll();
            var model = new PagedList<NewsType>(pagedList.ToList(), pageIndex, pageSize, pagedList.TotalItemCount);

            return View(model);
        }


        public ActionResult Save(int? id)
        {
            ViewData["roottypelist"] = RootNewsTypeEnum.支付方式.ToSelectList();

            var model = new SaveNewsType();

            if (id != null)
            {
                var item = _newstypeTask.GetById((int)id);
                if (item == null)
                    return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                model = EntityMapper.Map<NewsType, SaveNewsType>(item);
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Save(SaveNewsType savemodel)
        {


            if (ModelState.IsValid)
            {
                if (savemodel.ID == null)
                {
                    var model = new NewsType
                                    {
                                        TypeName = savemodel.TypeName,
                                        Sort = savemodel.Sort,
                                        CreateDate = DateTime.Now,
                                        ParentType = savemodel.ParentType,
                                    };
                    _newstypeTask.Add(model);
                }
                else
                {
                    var model = _newstypeTask.GetById((int)savemodel.ID);

                    if (model == null)
                        return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);


                    model.TypeName = savemodel.TypeName;
                    model.Sort = savemodel.Sort;
                    model.ParentType = savemodel.ParentType;


                    _newstypeTask.Update(model);
                }
                return AlertMsg("保存成功", HttpContext.Request.UrlReferrer.PathAndQuery);
            }
            return View(savemodel);
        }

        public ActionResult GetNewsTypeList(int typeid)
        {
            var list = _newstypeTask.GetAll(null, "ParentType = " + typeid.ToString()).Select(c => new SelectListItem
            {
                Text = c.TypeName,
                Value = c.ID.ToString(),
            }).ToList();
            list.Insert(0,new SelectListItem
                              {
                                  Selected = true,
                                  Text = "请选择",
                                  Value = "",
                              });
            return Json(list, JsonRequestBehavior.AllowGet);
        }





        #region 删除用户信息 Delete

        [HttpPost]
        public void Delete(int id)
        {
            _newstypeTask.Delete(id);
        }

        #endregion
    }
}